//
//  SettingDetailTableViewCell.m
//  DoorBell
//
//  Created by Eques-imac on 2017/7/29.
//  Copyright © 2017年 Eques-imac. All rights reserved.
//

#import "SettingDetailTableViewCell.h"

@implementation SettingDetailTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(UIImageView *)selectediMage{
    if (_selectediMage == nil) {
         _selectediMage = [UIImageView new];
        [self.contentView addSubview:_selectediMage];
        [_selectediMage mas_makeConstraints:^(MASConstraintMaker *make){
            make.width.mas_equalTo(20);
            make.height.mas_equalTo(20);
            make.right.equalTo(self.mas_right).offset(-28);
            make.centerY.equalTo(self.mas_centerY);
        }];
    }
    return _selectediMage;
}

-(UIImageView *)tipImages{
   if (!_tipImages) {
         _tipImages = [[UIImageView alloc]init];
        [self.contentView addSubview:_tipImages];
        [_tipImages mas_makeConstraints:^(MASConstraintMaker *make){
            make.width.mas_equalTo(40);
            make.height.mas_equalTo(20);
            make.left.equalTo(self.titleLabel.mas_right).offset(10);
            make.centerY.equalTo(self.mas_centerY);
        }];
    }
    return _tipImages;
}


- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
         _titleLabel = [[UILabel alloc]init];
         _titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16.0];
         _titleLabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
        [self.contentView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make){
            if (_detailLabel) {
                make.height.mas_equalTo(18);
                make.top.equalTo(self.mas_top).offset(8);
                make.left.equalTo(self.mas_left).offset(28);
            }else{
                    make.height.mas_equalTo(18);
                    make.centerY.equalTo(self.mas_centerY);
                    make.left.equalTo(self.mas_left).offset(28);
            }
            
            
        }];
    }
    return _titleLabel;
}


- (UILabel *)detailLabel{
    if (_detailLabel == nil) {
         _detailLabel = [UILabel new];
         _detailLabel.textColor = [UIColor grayColor];
         _detailLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
         _detailLabel.numberOfLines = 0;
        [_detailLabel sizeToFit];
        [self.contentView addSubview:_detailLabel];
        [_detailLabel mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.equalTo(self.mas_left).offset(28);
            make.bottom.equalTo(self.mas_bottom).offset(-11);
        }];
    }
    return _detailLabel;
}

- (UISwitch *)switchView{
     if (_switchView == nil) {
         _switchView = [[UISwitch alloc]init];
         _switchView.onTintColor = WXHColor(249, 141, 62, 1.0);
        [_switchView addTarget:self action:@selector(SwitchOpen:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_switchView];
        [_switchView mas_makeConstraints:^(MASConstraintMaker *make){
            make.right.equalTo(self.contentView.mas_right).offset(-28);
            make.top.equalTo(self.contentView.mas_top).offset(15);
            make.width.mas_equalTo(44);
            make.height.mas_equalTo(20);
        }];
    }
    return _switchView;
}



- (void)SwitchOpen:(UISwitch*)switchView
{
    if (_swtichvalue) {
        _swtichvalue(switchView.isOn);
    }
}



- (void)setModel:(YKSettingDetailModle *)model
{
    _model = model;
    
    if (model.isHaveSwitch) {
        self.switchView.on = model.openStatus;
        
        if (_selectediMage) {
           [_selectediMage removeFromSuperview];
           _selectediMage = nil;
        }
        
    }else{
        if (_switchView) {
            [_switchView removeFromSuperview];
             _switchView = nil;
        }
        
         if (_selectediMage) {
            [_selectediMage removeFromSuperview];
            _selectediMage = nil;
         }
        
    }
    
    if (_model.detailTitle) {
         self.detailLabel.text = _model.detailTitle;
    }else{
         if (_detailLabel) {
            [_detailLabel removeFromSuperview];
             _detailLabel = nil;
        }
    }

   if (_model.title) {
        self.titleLabel.text = _model.title;
        [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make){
            if (_detailLabel) {
                make.height.mas_equalTo(18);
                make.top.equalTo(self.mas_top).offset(8);
                make.left.equalTo(self.mas_left).offset(28);
            }else{
                make.height.mas_equalTo(18);
                make.centerY.equalTo(self.mas_centerY);
                make.left.equalTo(self.mas_left).offset(28);
           }
        }];
    }

}



@end
